package com.caigou.modules.personal.ui.info

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.caigou.comm.base.bean.AppBackgroundInfo
import com.caigou.comm.base.container.BaseFragment
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.ext.observe
import com.caigou.comm.base.ext.showToast
import com.caigou.comm.base.widget.SpaceItemDecoration
import com.caigou.modules.personal.R
import com.caigou.modules.personal.adapter.GrowthVoItemAdapter
import com.caigou.modules.personal.bean.GrowthVoBean
import com.caigou.modules.personal.databinding.FragmentGrowthVoBinding
import com.caigou.modules.personal.viewmodel.PersonalInfoViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/12/01
 */
@AndroidEntryPoint
class GrowthVoFragment : BaseFragment() {

    private var _binding: FragmentGrowthVoBinding? = null
    private val binding get() = _binding!!

    private val personalInfoViewModel: PersonalInfoViewModel by viewModels()

    private var page = 1
    private val limit = 10
    private var totalPage = 0

    /**
     * RecyclerView Divider
     */
    private val recyclerViewItemDecoration by lazy {
        SpaceItemDecoration(requireActivity())
    }

    private val linearLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(requireActivity())
    }

    private val mAdapter: GrowthVoItemAdapter by lazy {
        GrowthVoItemAdapter(mutableListOf())
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentGrowthVoBinding.inflate(inflater, container, false)
        return _binding?.root
    }

    override fun initView() {
        binding.growthVoSwipeRefreshLayout.run {
            setColorSchemeColors(resources.getColor(R.color.caigou_theme_color))
            setOnRefreshListener {
                mAdapter.loadMoreModule.isEnableLoadMore = false
                page = 1
                personalInfoViewModel.getGrowthValueList(page, limit, AppBackgroundInfo.instance.userBean!!.userId)
            }
        }
        mAdapter.loadMoreModule.run {
            setOnLoadMoreListener {
                mAdapter.loadMoreModule.isEnableLoadMore = true
                personalInfoViewModel.getGrowthValueList(page, limit, AppBackgroundInfo.instance.userBean!!.userId)
            }
            isAutoLoadMore = true
            isEnableLoadMoreIfNotFullPage = false
        }
        binding.growthVoRecyclerView.run {
            layoutManager = linearLayoutManager
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(recyclerViewItemDecoration)
        }
        binding.growthVoTvBack.setOnClickListener {
            (requireActivity() as PersonalInfoActivity).onBackPressed()
        }
    }

    override fun lazyLoad() {
        personalInfoViewModel.getGrowthValueList(page, limit, AppBackgroundInfo.instance.userBean!!.userId)
    }

    override fun onBack() {
    }

    override fun observeViewModel() {
        observe(personalInfoViewModel.growthVoLiveData, ::handleGetGrowthValueList)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    private fun handleGetGrowthValueList(result: Resource<GrowthVoBean>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                binding.growthVoSwipeRefreshLayout.isRefreshing = false
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("获取团队信息异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                binding.growthVoSwipeRefreshLayout.isRefreshing = false
                mAdapter.loadMoreModule.isEnableLoadMore = true

                if (result.data == null) {
                    return
                }

                if (page == 1) {
                    mAdapter.setList(result.data!!.page.list)
                } else {
                    mAdapter.addData(result.data!!.page.list)
                }

                if (result.data!!.page.pageSize < limit) {
                    mAdapter.loadMoreModule.loadMoreEnd(false)
                } else {
                    mAdapter.loadMoreModule.loadMoreComplete()
                }

                if (page >= totalPage) {
                    mAdapter.loadMoreModule.loadMoreEnd(false)
                } else {
                    page++
                }
            }
        }
    }
}